package com.nsgf.library.ui.page.home;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;

import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.kunminx.architecture.ui.state.State;
import com.nsgf.library.BR;
import com.nsgf.library.R;
import com.nsgf.library.data.bean.UserInfo;
import com.nsgf.library.event.EventMsg;
import com.nsgf.library.ui.UserInfoActivity;
import com.nsgf.library.ui.page.BaseFragment;
import com.nsgf.library.ui.page.ModifyPassWordActivity;
import com.nsgf.library.ui.page.StateHolder;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class MineFragment extends BaseFragment {

    private MineFragmentStates mStates;

    @Override
    protected void initViewModel() {
        mStates = getActivityScopeViewModel(MineFragmentStates.class);
    }

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        return new DataBindingConfig(R.layout.fragment_nsgf_mine, BR.vm, mStates)
            .addBindingParam(BR.click, new ClickProxy());
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        EventBus.getDefault().register(this);
    }

    public static class MineFragmentStates extends StateHolder {

        public final State<String> name = new State<>(UserInfo.getInstance().getName());
        public final State<String> realName = new State<>(UserInfo.getInstance().getRealName());
        public final State<Boolean> loadingVisible = new State<>(false);

    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(EventMsg event) {
        if ("warning".equals(event.getMsg())) {
            mStates.projectName.set(UserInfo.getInstance().getProjectName());
        }
    }
    public class ClickProxy {

        public void logout() {
            UserInfo.getInstance().clearUserInfo();
            getActivity().finish();
        }
        public void openInfo(){
            startActivity(new Intent().setClass(getActivity(), UserInfoActivity.class));
        }

        public void modifyPass() {

            startActivity(new Intent().setClass(getActivity(), ModifyPassWordActivity.class));
        }
    }
}
